<template>
	<view class="sheheimu">
		<view class="heimusj">
			<view class="hei_title">提示</view>
			<u-image style="margin-top: 82rpx;" width="200rpx" height="200rpx" :src="common.imgurl('upLogo')"></u-image>
			<view class="updata_font">{{show ? '发现新的版本' : '当前版本过低' }}</view>
			<view style="width: 80%; margin-top:20rpx;" v-if="percent > 0">
				<u-line-progress active-color="#EE8F57" :percentage="percent"></u-line-progress>
			</view>
			<view class="lijigenx" @click="appdow">{{show ? '立即更新' : '前往下载'}}</view>
		</view>
		<u-toast ref="cuupdata" />
	</view>
</template>

<script>
	import {reqappandrod} from '@/api/index.js';
	export default {
		props: {
			// 热更新文件地址
			udatafilePath: {
				type: String,
				default: undefined
			},
			// 强制跟新文件地址
			filePath: {
				type: String,
				default: undefined
			},
			show: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				percent: 0,
				allow: true
			};
		},
		methods: {
			appdow: async function() {
				let that = this;
				let percent = this.percent;
				if (that.udatafilePath == '') {
					this.allow = false
				}
				if (!this.allow) {
					// 强制更新
					if(plus.os.name=="Android"){
						uni.showLoading({
							title: '更新中……'
						});
						var downloadTask = uni.downloadFile({
							url: that.filePath,
							success: downloadResult => {
								if (downloadResult.statusCode === 200) {
									plus.runtime.install(
										//安装
										downloadResult.tempFilePath, {
											force: false
										},
										function() {
											plus.runtime.restart();
										}
									);
								}
							},
							complete: (res) => {
								uni.hideLoading();
							}
						});
						downloadTask.onProgressUpdate(data2 => {
							that.percent = data2.progress;
						});
					}else{
						plus.runtime.openURL(that.filePath)
					}
				}
				// 热更新
				if (percent > 0) {
					this.$refs.cuupdata.show({
						title: '请等待下载完成',
						type: 'error'
					});
				} else {
					var file = that.udatafilePath
					if (file) {
						uni.showLoading({
							title: '更新中……'
						});
						var downloadTask = uni.downloadFile({
							url: file,
							success: downloadResult => {
								if (downloadResult.statusCode === 200) {
									uni.showModal({
										title: '',
										content: '更新成功，确定现在重启吗？',
										confirmText: '重启',
										confirmColor: '#EE8F57',
										success: function(res) {
											if (res.confirm) {
												plus.runtime.install(
													//安装
													downloadResult.tempFilePath, {
														force: true
													},
													function() {
														plus.runtime.restart();
													}
												);
											}
										}
									});
								}
							},
							complete: () => {
								uni.hideLoading();
							}
						});
						downloadTask.onProgressUpdate(data2 => {
							that.percent = data2.progress;
						});
					}
				}
			}
		}
	}
</script>

<style lang="stylus">
.sheheimu
	position fixed;
	min-height 100vh
	width 100vw
	background rgba(1, 1, 1, 0.5)
	z-index 100000000
	display flex
	justify-content center
	align-items center
	top 0
	left 0
	.heimusj
		width 700rpx
		min-height 450rpx
		background #FFFFFF
		border-radius 16rpx
		display flex
		flex-direction column
		align-items center
		padding-bottom 50rpx
		position: absolute;
		top: 25vh;
		.hei_title{
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 549;
			color: #474443;
			line-height 1
			margin-top 82rpx
		}
		.updata_font{
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #474443;
			line-height 1
			margin-top 49rpx
		}
		.heititle
			width 266rpx
			height 312rpx
			
		.banbenzs
			font-size 32rpx
			font-family PingFang SC
			font-weight bold
			color #0081D6
			line-height 1
			z-index 100
			margin-top -26rpx
			letter-spacing 6rpx
		.bangexnei
			font-size 26rpx
			font-family PingFang SC
			font-weight 500
			color #323232
			width 100%
			padding-left 43rpx
			line-height 1
			margin-top 14rpx
		.genneiiten
			margin-top 18rpx
			line-height 1 !important
			font-size 26rpx
			font-family PingFang SC
			font-weight 400
			display flex
			flex-direction column
			color #323232
		.lijigenx
			width 255rpx
			height 53rpx
			background #D9B79C
			border-radius 16rpx
			line-height 53rpx
			text-align center
			margin-top 90rpx
			font-size 30rpx
			font-family PingFang SC
			font-weight 500
			color #FFFFFF
			
	image
		width 100%
		height 100%
</style>